﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Models;
using DAL;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Transactions;
using System.Xml.Linq;

namespace BLL
{
    public class B_Group
    {
        D_Group dal = new D_Group();

        //这里注入了一个D_G dal层对象,但是我们没有D_G, 和g_data, 我也忘了g_data重新命名成什么了
        //t_data 对应TeaChoice
        //g_data 对应StuChoice

        D_StuChoice stuChoice = new DAL.D_StuChoice();

        public List<StuGroup> GetList()
        {
            return dal.getList();
        }

        public bool SaveList(List<StuGroup> groups)
        {
            return dal.saveList(groups);
        }

        public StuGroup GetGroup(Student student)
        {
            return dal.getGroup(student);
        }

        public bool UpdateGroup(StuGroup group)
        {
            var res = dal.update(group);
            return res;
        }

        public bool Release(StuGroup stu_Group)
        {
            try
            {
                using (TransactionScope tx = new TransactionScope())//事务处理
                {
                    dal.release(stu_Group);//删除组
                    stuChoice.del(stu_Group);//删除组选择信息
                    BLL.B_Student student = new BLL.B_Student();
                    int rows = student.ReleaseGroupId(stu_Group);

                    if (rows != stu_Group.members)
                        tx.Dispose();
                    else
                        tx.Complete();
                    // 这里有log info
#if true
                    LogInfo.student.is_gleader = 0;
                    LogInfo.student.group_id = 0;
#endif
                }
            }
            catch (SqlException ex)
            {
                int error_code = ex.ErrorCode;
                // 不重要的代码
                //throw new Tools.AddException(string.Format("添加失败！ sql 错误代码{0}", error_code));
            }
            catch (Exception e)
            {
                throw e;
            }

            return true;
        }

        public bool Commit(StuGroup stu_Group, Student student)
        {
            D_Student d_student = new DAL.D_Student();

            if (student.is_gleader == 1)
            {
                dal.update(stu_Group);
                return true;
            }

            if (student.group_id == 0)
            {
                using (TransactionScope tx = new TransactionScope())//事务处理
                {
                    stu_Group.members = 1;
                    stu_Group.group_lead = student.stu_id;
                    int group_id = dal.add(stu_Group);
                    student.group_id = group_id;
                    student.is_gleader = 1;
                    d_student.updateGroupid(student);
                    tx.Complete();
                    stu_Group.group_id = group_id;
                }

                return true;
            }
            else
            { return false; }
        }

        public bool AddStudent(List<Student> group_student, StuGroup group)
        {
            B_Student b_Student = new B_Student();
            B_Group b_Group = new B_Group();
           

            bool res;

            using (TransactionScope tx = new TransactionScope())//事务处理
            {
                
                res = b_Student.UpdateGroupId(group_student, group.group_id);
                if (!res) tx.Dispose();

                group.members += group_student.Count;
                b_Group.UpdateGroup(group);
                tx.Complete();// 无法释放
                res = true;
            }

            return res;
        }

        public bool RemoveStudent(List<Student> group_student, StuGroup group)
        {
            BLL.B_Student b_Student = new BLL.B_Student();
            BLL.B_Group b_Group = new BLL.B_Group();
           

            bool res;

            using (TransactionScope tx = new TransactionScope())//事务处理
            {
                res = b_Student.UpdateGroupId(group_student, 0);
                if (!res) tx.Dispose();

                group.members -= group_student.Count;
                b_Group.UpdateGroup(group);
                tx.Complete();
                res = true;
            }

            return res;
        }
    }
}
